<?php

namespace App\Service;

use App\Repositories\Category as CategoryRepository;
use Illuminate\Http\Request;

class Category
{
    use ServiceHelper;
    //
    const DISPLAY = 1;
    const DISPLAY_NONE = 2;

    private $request;

    public static function getStatusList(){
        return [
            self::DISPLAY   =>  '显示',
            self::DISPLAY_NONE  =>  '隐藏'
        ];
    }

    public static function getCatMod(){
        return [
            1   =>  '文章 | article',
            2   =>  '图片 | image',
            3   =>  '文件 | file',
            4   =>  '视频 | video'
        ];
    }
    public static function getCatAttr(){
        return [
            0   =>  '列表',
            1   =>  '封面'
        ];
    }
    public static function getCatCross(){
        return [
            0   =>  '不交叉',
            1   =>  '自动获取同名栏目内容',
            2   =>  '手工指定交叉栏目ID'
        ];
    }
    /**
     * @param $status
     *
     * @return mixed
     */
    public function getStatusString($status){
        $statusList =  $this->getStatusList();
        return $statusList[$status];
    }

    /**
     * @param $pid
     * @param bool $bool
     *
     * @return mixed
     */
    public static function getSon($pid, $bool=false){
        if($bool){
            return self::where('pid',$pid)->where('status',self::DISPLAY)->get();
        }
        return self::where('pid',$pid)->get();
    }

    public static function getParent($id,$bool=false){
        if($bool){
            return self::where('id',$id)->where('status',self::DISPLAY)->get();
        }
        return self::where('id',$id)->get();
    }
    /**
     * @param $id
     *
     * @return string
     */
    public function getName($id){
        $id = (int)$id;
        if($id<1){
            return '已为顶级栏目';
        }else{
            $name = CategoryRepository::select('cat_name')->where('id',$id)->first();
            if($name){
                return $name->cat_name;
            }else{
                return '';
            }
        }
    }


    /**
     * @param $pid
     * @param int $level
     *
     * @return bool
     */
    public function getParentId($pid, $level=1){
        if($pid==0){
            return $pid;
        }
        if($level==1){
            $model = CategoryRepository::select('pid')->where('id',$pid)->first();
            if($model){
                return $model->pid;
            }else{
                return false;
            }
        }
    }


    /**
     * @param Request $request
     *
     * @return $this|\Illuminate\Database\Eloquent\Builder|static
     */
    public function getPageList(Request $request){
        if(isset($request->pid)&&$request->pid){
            $model = CategoryRepository::where('pid',$request->pid);
        }else{
            $model = CategoryRepository::where('pid',0);
        }
        if(isset($request->status)&&$request->status){
            $model = $model->where('status',$request->status);
        }
        //状态
        $model=$model->paginate($request->limit)
            ->toArray();
        foreach ($model['data'] as $k=>&$v){
            $v['parent_name'] = $this->getName($v['pid']);
            $v['top_name'] = $this->getName($v['topid']);
            $v['status_name'] = $this->getStatusString($v['status']);
            $v['ppid'] = $this->getParentId($v['pid']);

        }
        return $model;
    }


    public static function getDisplay($length=4){
        $res = CategoryRepository::where('status',self::DISPLAY)->limit(4)->get();
        if($res){
            return $res->toArray();
        }else{
            return [];
        }
    }

    public function getByIspart($html = true,$default='',$ispart=1){
        switch ($ispart){
            case 1:
                $res = CategoryRepository::where('ispart',1)->get();
                break;
            case 0:
                $res = CategoryRepository::all();
                break;
            default:
                $res = CategoryRepository::where('ispart',1)->get();
                break;
        }
        if($res){
            $res = getTree($res->toArray(),'pid');
        }else{
            $res = [];
        }
        if($html){
            if($ispart==0){
                return exportTree($res,'cat_name',0,$default,1,'ispart');
            }
            return exportTree($res,'cat_name',0,$default);

        }else{
            return $res;
        }
    }







    public function draft($id = []){
        if($id == []){
            CategoryRepository::where('status',self::DISPLAY)->update(['status'=>self::DISPLAY_NONE]);
        }else{
            if(is_array($id)){
                CategoryRepository::whereIn('id',$id)->where('status',self::DISPLAY)->update(['status'=>self::DISPLAY_NONE]);
            }else{
                CategoryRepository::where('id',$id)->where('status',self::DISPLAY)->update(['status'=>self::DISPLAY_NONE]);

            }
        }
    }

    /**
     * @param array $id
     */
    public function published($id = []){
        if($id == []){
            CategoryRepository::where('status',self::DISPLAY_NONE)->update(['status'=>self::DISPLAY]);
        }else{
            if(is_array($id)){
                CategoryRepository::whereIn('id',$id)->where('status',self::DISPLAY_NONE)->update(['status'=>self::DISPLAY]);
            }else{
                CategoryRepository::where('id',$id)->where('status',self::DISPLAY_NONE)->update(['status'=>self::DISPLAY]);

            }
        }
    }

    public function sortCol($id = [],$sort = 50){
        if($id == []){
        }else{
            if(is_array($id)){
                CategoryRepository::whereIn('id',$id)->where('sort','<>',$sort)->update(['sort'=>$sort]);
            }else{
                CategoryRepository::where('id',$id)->where('sort','<>',$sort)->update(['sort'=>$sort]);

            }
        }
    }

    /**
     * @param array $id
     */
    public function del($id = []){
        if($id == []){

        }else{
            if(is_array($id)){
                CategoryRepository::whereIn('id',$id)->delete();
            }else{
                CategoryRepository::where('id',$id)->delete();

            }
        }
    }


    /**
     * @param $post
     */
    public function insertOrUpdate($post){
        if(isset($post['id'])){
            $model = CategoryRepository::find($post['id']);
        }else{
            $model = new CategoryRepository();
        }
        $model->cat_name = $post['cat_name'];
        $model->slug = $post['slug']?$post['slug']:'';
        if($post['pid']){
            //是否设置父栏目
            $pid = $post['pid'];
            //首先判断是否为顶级栏目
            $model->pid = $pid;//设置父id
            $parent = self::getParent($pid);//查询父栏目是否存在
            if($parent){
                //如果存在
                if($parent['pid']){
                    //父栏目是不为顶级栏目、那么topid就是父栏目id
                    $model->topid = $parent['topid'];
                }else{
                    $model->topid = $pid;
                }
            }else{
                //如果父栏目出错，即用户操作错误默认设置为顶级栏目
                $model->pid = 0;
                $model->topid = 0;
            }
        }else{
            $model->pid = 0;
            $model->topid=0;
        }
        $model->ispart = $post['ispart'];
        $model->cross = isset($post['cross'])?$post['cross']:0;
        $model->crossid = $post['crossid'];
        $model->status = isset($post['status'])?$post['status']:self::DISPLAY_NONE;
        $model->desc = $post['desc'];
        $model->keywords = $post['keywords'];
        $model->tempindex = $post['tempindex'];
        $model->templist = $post['templist'];
        $model->temparticle = $post['temparticle'];
        $model->sort = $post['sort']||$post['sort']===0?$post['sort']:50;
        $model->save();
    }

}
